package com.example.casper.Experiment2024;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.IOException;

public class BookItemDetailActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_ALBUM = 100;
    private static final int PICK_IMAGE_REQUEST = 1;
    private TextView textViewName;
    private TextView textViewData;
    private ImageView image_op;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_book_item_detail);
        Intent intent=this.getIntent();
        String name= intent.getStringExtra("item_name");
        image_op=findViewById(R.id.item_view);
        textViewName = findViewById(R.id.edit_text_item_name);
        textViewData = findViewById(R.id.edit_text_item_data);
        if(null!=name)
        {
            String data=intent.getStringExtra("item_data");
            textViewName.setText(name);
            textViewData.setText(data);
        }
        Log.i("Book.ItemActivity", "the intent take a key-value"+intent.getStringExtra("key"));

        Button buttonConfirm= findViewById(R.id.button_confirm);
        buttonConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnIntent = new Intent();
                Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                //returnIntent.putExtra("position", intent.getIntExtra("position",0));
                returnIntent.putExtra("item_name", textViewName.getText().toString());
                returnIntent.putExtra("item_data", textViewData.getText().toString());
                returnIntent.putExtra("bitmap", bitmap);
                setResult(RESULT_OK, returnIntent);
                BookItemDetailActivity.this.finish();
            }
        });

//        image_op.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(Intent.ACTION_PICK,
//                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//                intent.setType("image/*");
//                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
//                intent.setAction(Intent.ACTION_GET_CONTENT);
//                startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE_ALBUM);
//
//            }
//        });
        image_op.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent chooseImageIntent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                chooseImageIntent.setType("image/*");
                chooseImageIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
                startActivityForResult(Intent.createChooser(chooseImageIntent, "选择图片"), REQUEST_CODE_ALBUM);

            }
        });

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ALBUM && resultCode == RESULT_OK) {
            if (data != null && data.getData() != null) {
                Uri selectedImageUri = data.getData(); // 获取选择的图片 URI
                try {
                    // 获取 Bitmap 并将其设置到 ImageView
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
                    image_op.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(this, "加载图片时出现错误", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}